package com.ruoyi.common.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonValue;
import com.ruoyi.common.annotation.EnumConverter;
import com.ruoyi.common.utils.StringUtils;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.apache.commons.compress.utils.Lists;
import org.springframework.core.convert.converter.Converter;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * 工作人员类型
 * 领导小组，禁毒专职社工，戒毒专干
 * 吸毒人员类型
 * 社戒社康，重点关注人员，康复救助人员，查获登记人员，双列管人员，强戒人员
 */
@Getter
@AllArgsConstructor
@JsonFormat(shape = JsonFormat.Shape.STRING)
public enum UserBusinessSubType {

    /**
     * 领导小组
     */
    MEMBER_LDXZ("member_ldxz"),

    /**
     * 专职社工
     */
    MEMBER_ZZSG("member_zzsg"),

    /**
     * 戒毒专干
     */
    MEMBER_JDZG("member_jdzg"),

    /**
     * 社戒社康
     */
    DRUG_SJSK("drug_sjsk"),

    /**
     * 重点关注人员
     */
    DRUG_ZDGZ("drug_zdgz"),

    /**
     * 康复救助
     */
    DRUG_KFJZ("drug_kfjz"),

    /**
     * 查获登记
     */
    DRUG_CHDJ("drug_chdj"),

    /**
     * 双列管
     */
    DRUG_SLG("drug_slg"),

    /**
     * 强戒人员
     */
    DRUG_QJ("drug_qj");

    @EnumValue
    private final String userType;

    public List<String> getMemberTypeList() {
        switch (this) {
            case MEMBER_LDXZ:
                return Collections.singletonList("0");
            case MEMBER_JDZG:
                return Collections.singletonList("1");
            case MEMBER_ZZSG:
                return Collections.singletonList("2");
            default:
                return Lists.newArrayList();
        }
    }

    public List<String> getDrugTypeList() {
        switch (this) {
            case DRUG_SJSK:
                return Arrays.asList("0", "1");
            case DRUG_ZDGZ:
                return IntStream.range(2, 19).boxed().map(Object::toString).collect(Collectors.toList());
            case DRUG_CHDJ:
                return Collections.singletonList("18");
            case DRUG_QJ:
                return Collections.singletonList("2");
            case DRUG_KFJZ:
                return Collections.singletonList("19");
            case DRUG_SLG:
                return Collections.singletonList("20");
            default:
                return Lists.newArrayList();
        }
    }

    @JsonValue
    public String getUserType() {
        return userType;
    }

    @EnumConverter
    public static class InnerConverter implements Converter<String, UserBusinessSubType> {
        @Override
        public UserBusinessSubType convert(String source) {
            return getUserType(source);
        }

    }

    public static UserBusinessSubType getUserType(String userType, UserBusinessType type) {
        for (UserBusinessSubType value : values()) {
            if (UserBusinessType.USER_DRUG.equals(type) && value.getDrugTypeList().contains(userType)) {
                return value;
            }
            if (UserBusinessType.USER_MEMBER.equals(type) && value.getMemberTypeList().contains(userType)) {
                return value;
            }
        }
        return null;
    }

    public static UserBusinessSubType getUserType(String str) {
        for (UserBusinessSubType value : values()) {
            if (StringUtils.contains(str, value.getUserType())) {
                return value;
            }
        }
        throw new RuntimeException("'UserBusinessType' not found By " + str);
    }

}
